package cre.gutwo.swing;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cre.gutwo.service.RuleService;
import cre.gutwo.util.SpringContextUtil;

/**
 * 
 * @author __USER__
 */
public class InitCompareFrame extends javax.swing.JFrame implements ActionListener, ListSelectionListener {
	private static final long serialVersionUID = 9179264493002935036L;

	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;

	private javax.swing.JLabel jLabel1;
	private JLabel jLabel2;
	private JTextField textField1;
	private JTextField textField2;

	private RuleService ruleService;

	/** Creates new form ZhuaTu */
	public InitCompareFrame() {
		initComponents();
		init();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		// setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		// label
		jLabel1 = new javax.swing.JLabel();
		jLabel1.setText("DeviceInfo");

		jLabel2 = new JLabel();
		jLabel2.setText("CompanyCode");
		// button
		jButton1 = new javax.swing.JButton("生成对照表");
		// 文件选择器
		textField1 = new JTextField();
		textField1.setColumns(10);

		textField2 = new JTextField();
		textField2.setColumns(10);

		jButton2 = new JButton("浏览");
		jButton3 = new JButton("浏览");

		initLayout();

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	private void initLayout() {
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(Alignment.LEADING)
								.addGroup(layout.createSequentialGroup().addContainerGap()
										.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(jLabel1)
												.addComponent(jLabel2))
								.addGap(41)
								.addGroup(layout.createParallelGroup(Alignment.LEADING, false).addComponent(textField2)
										.addComponent(textField1, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(jButton3)
								.addComponent(jButton2)))
						.addGroup(layout.createSequentialGroup().addGap(173).addComponent(jButton1)))
				.addContainerGap(237, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addGap(32)
						.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(jLabel1)
								.addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(jButton2))
				.addGap(29)
				.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(jLabel2)
						.addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(jButton3)).addPreferredGap(ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
				.addComponent(jButton1).addGap(50)));
		getContentPane().setLayout(layout);
	}

	public void init() {
		jButton1.setActionCommand("init");
		jButton1.addActionListener(this);

		jButton2.setActionCommand("file1");
		jButton2.addActionListener(this);

		jButton3.setActionCommand("file2");
		jButton3.addActionListener(this);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				InitCompareFrame hf = new InitCompareFrame();
				hf.setTitle("对照表初始化工具");
				hf.setLocationRelativeTo(null);
				hf.setSize(500, 280);
				hf.setVisible(true);
			}
		});
	}

	// End of variables declaration//GEN-END:variables
	@Override
	public void valueChanged(ListSelectionEvent e) {

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if ("init".equals(command)) {
			String diPath = textField1.getText();
			String ccPath = textField2.getText();

			if (StringUtils.isBlank(diPath) && StringUtils.isBlank(ccPath))
				return;

			if (SpringContextUtil.getApplicationContext() == null) {
				new ClassPathXmlApplicationContext("applicationContext.xml");
			}

			if (ruleService == null)
				ruleService = SpringContextUtil.getBean("ruleServiceImpl", RuleService.class);

			if (StringUtils.isNotBlank(diPath)) {
				ruleService.initDeviceInfo(diPath);
				JOptionPane.showMessageDialog(this, "DeviceInfo数据文件初始化成功");
			}

			if (StringUtils.isNotBlank(ccPath)) {
				ruleService.initCompanyCode(ccPath);
				JOptionPane.showMessageDialog(this, "CompanyCode数据文件初始化成功");
			}

		} else if ("file1".equals(command)) {
			textField1.setText(gainFilePath(fileChooser()));
		} else if ("file2".equals(command)) {
			textField2.setText(gainFilePath(fileChooser()));
		}
	}

	protected String gainFilePath(File file) {
		if (file != null)
			return file.getAbsolutePath();

		return null;
	}

	protected File fileChooser() {
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		File result = null;

		// 打开文件选择窗口
		JFileChooser chooser = new JFileChooser();
		chooser.setFileFilter(gainFileFilter());

		int r = chooser.showOpenDialog(this);
		if (r == JFileChooser.APPROVE_OPTION) {
			result = chooser.getSelectedFile();
		} else {
			// 没有选择文件
		}
		setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		return result;
	}

	FileFilter gainFileFilter() {
		return new FileNameExtensionFilter("excel文件", "xls", "xlsx");
	}
}